import os

import numpy as np
from gxl_ai_utils.utils import utils_file

# cv handler
input_list_dir = '/home/work_nfs8/xlgeng/new_workspace/gxl_ai_utils/eggs/cats_and_dogs/prepare_data_for_salmonn_TTS/wenetspeech_acoustic/wtn_cv.list'
wav_dict = {}
text_dict = {}
token_dict = {}
dict_list = utils_file.load_dict_list_from_jsonl(input_list_dir)
for dict_i in dict_list:
    text_dict[dict_i['key']] = dict_i['txt']
    wav_dict[dict_i['key']] = dict_i['wav']
    npy_path = dict_i['npy']
    token_list = np.load(npy_path).tolist()
    tokens_str = ' '.join([str(i) for i in token_list])
    token_dict[dict_i['key']] = tokens_str

# 下载音频到nfs
utils_file.logging_print('start download wav to nfs')
output_dir = '/home/work_nfs8/xlgeng/new_workspace/gxl_ai_utils/eggs/cats_and_dogs/prepare_data_for_salmonn_TTS/wav/cv_wav'
utils_file.makedir(output_dir)
wav_path_list = list(wav_dict.values())
runner = utils_file.GxlFixedThreadPool(16)
runner.map(utils_file.copy_file2, wav_path_list, other_fun_args={"target_dir": output_dir})
runner.start()
utils_file.logging_print('end download wav to nfs')
new_wav_dict = {}
for key, value in wav_dict.items():
    new_wav_dict[key] = os.path.join(output_dir, os.path.basename(value))
utils_file.write_dict_to_scp(new_wav_dict, './cv_wav.scp')
utils_file.write_dict_to_scp(text_dict, './cv_text.scp')
utils_file.write_dict_to_scp(token_dict, './cv_token.scp')
new_wav_dict_list = []
for dict_i in dict_list:
    dict_i['wav'] = new_wav_dict[dict_i['key']]
    new_wav_dict_list.append(dict_i)

utils_file.write_dict_list_to_jsonl(new_wav_dict_list, './cv_wav.list')



